Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

301
Visualizações
check whether a given property type is a typeof(list<>)

I have three lists something like List<EmpRoles> , List<EmpVisibility> , List<EmpProps>.

Now I want to perform certain operations on them. For this first I have to check whether the property is of type list or not. I have use if block something like below

if ( propertyName == "EmpRoles" || propertyName == "EmpVis" || propertyName == "EmpProps") 

Is there any better way of doing this thing , or is it possible to put some typeof(list<>) conditions. I know typeof(list<>) won't work here. Either i have to use typeof(list)... Can Someone help in making a generic way to identify the list type properties?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Checking that a type is a List<T> requires ensuring that:

  • the type is generic by checking its IsGenericType property,
  • the generic type base is System.List<> by checking the result of GetGenericTypeDefinition() against typeof(List<>)
  • type T is the one you want by checking GetGenericArguments()

If all three conditions are met, you have your type:

var pt = myProperty.PropertyType;
if (pt.IsGenericType &&
    pt.GetGenericTypeDefinition() == typeof(List<>)) {
    var elementType = pt.GetGenericArguments()[0];
    if (elementType == typeof(EmpRoles)) {
        ...
    } else if (elenentType == typeof(EmpVisibility)) {
        ...
    } else if ...
}
over 4 years ago · Santiago Trujillo Relatório

0

You can get generic parameter from list as following

var list = new List<EmpRoles>
var argType = list.GetType().GenericTypeArguments[0]; 

Here in argType you will get typeof(EmpRoles)

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda